import random
from pymt import *
from pyglet.gl import *

from SelectableObject import *
from SelectionGesture import *

from ConnectableObject import *
from ConnectionGesture import *
from ModifiedWindow import *


class MTBox (MTSelectableObject):    
    adjustLetPositionX=20
    adjustLetPositionY=20
    
    def __init__(self, x,y):
        MTSelectableObject.__init__(self, x, y)
        #self.inlet  = MTConnectableObject (x-adjustLetPositionX, y-adjustLetPositionY)
        self.inlets  = []
        self.outlets = []
        
        #self.inlets.append(MTConnectableObject (x-self.adjustLetPositionX, y-self.adjustLetPositionY))
        #self.inlets.append(MTConnectableObject (x+self.adjustLetPositionX, y-self.adjustLetPositionY))


    '''
        falta:
        - fazer a integracao!!!
        - correr pro abraco!!!!
        - corrigir o algoritmo de auto organizacao de inlets/outlets (init_inlets e init_outlets)
    '''
    
    
    def calculate_distance (self, number):
        return (20*number)+(20*(number-1))
    
    
    
    
        
    def init_inlets (self, number):
        self.inlets=[]
        size=self.calculate_distance(number)
        #se a juncao dos leds e maior que a palavra escrita
        if size > self.width:
            initialX = self.x-self.adjustLetPositionX
            initialY = self.y+self.adjustLetPositionY
            for i in range(number):
                modification = self.calculate_distance(i)
                self.inlets.append(MTConnectableObject (initialX + modification, initialY))
        else:
            initialX = self.x-self.adjustLetPositionX
            initialY = self.y+self.adjustLetPositionY
            tamanho=self.width+(2*self.adjustLetPositionX)
            for i in range(number):
                if i>0:
                    modification=tamanho/i
                else:
                    modification=0
                self.inlets.append(MTConnectableObject (initialX + modification, initialY))
                
                
    def update_inlets (self):
        size=self.calculate_distance(len(self.inlets))
        
        if len(self.inlets)<=0:
            return
        
        #se a juncao dos leds e maior que a palavra escrita
        if size > self.width:
            initialX = self.x-self.adjustLetPositionX
            initialY = self.y+self.adjustLetPositionY
            for i in range(len(self.inlets)):
                modification = self.calculate_distance(i)
                self.inlets[i].x = initialX + modification
                self.inlets[i].y = initialY

        else:
            initialX = self.x-self.adjustLetPositionX
            initialY = self.y+self.adjustLetPositionY
            tamanho=self.width+(2*self.adjustLetPositionX)
            resto=tamanho-(len(self.inlets)*self.inlets[0].width)
            
            if len(self.inlets)-1!=0:
                modification=resto/(len(self.inlets)-1)
            else:
                modification=0
                
            for i in range(len(self.inlets)):
                self.inlets[i].x = initialX
                if i!=0:
                    self.inlets[i].x += modification 
                self.inlets[i].y = initialY
                initialX = self.inlets[i].x + self.inlets[i].width
        
    





    def init_outlets (self, number):
        self.outlets=[]
        size=self.calculate_distance(number)
        #se a juncao dos leds e maior que a palavra escrita
        if size > self.width:
            initialX = self.x-self.adjustLetPositionX
            initialY = self.y-self.adjustLetPositionY
            for i in range(number):
                modification = self.calculate_distance(i)
                self.outlets.append(MTConnectableObject (initialX + modification, initialY))
        else:
            initialX = self.x-self.adjustLetPositionX
            initialY = self.y-self.adjustLetPositionY
            tamanho=self.width+(2*self.adjustLetPositionX)
            for i in range(number):
                if i>0:
                    modification=tamanho/i
                else:
                    modification=0
                self.outlets.append(MTConnectableObject (initialX + modification, initialY))
                
                
    def update_outlets (self):
        size=self.calculate_distance(len(self.outlets))
        
        if len(self.outlets)<=0:
            return
        
        #se a juncao dos leds e maior que a palavra escrita
        if size > self.width:
            initialX = self.x-self.adjustLetPositionX
            initialY = self.y-self.adjustLetPositionY
            for i in range(len(self.outlets)):
                modification = self.calculate_distance(i)
                self.outlets[i].x = initialX + modification
                self.outlets[i].y = initialY

        else:
            initialX = self.x-self.adjustLetPositionX
            initialY = self.y-self.adjustLetPositionY
            tamanho=self.width+(2*self.adjustLetPositionX)
            resto=tamanho-(len(self.outlets)*self.outlets[0].width)
            
            if len(self.outlets)-1!=0:
                modification=resto/(len(self.outlets)-1)
            else:
                modification=0
                
            for i in range(len(self.outlets)):
                self.outlets[i].x = initialX
                if i!=0:
                    self.outlets[i].x += modification 
                self.outlets[i].y = initialY
                initialX = self.outlets[i].x + self.outlets[i].width
                
                
                
    
    
    def on_draw (self):
        MTSelectableObject.on_draw(self) 
        
        #comment this line
        #self.inlet.on_draw()
        self.update_inlets()
        self.update_outlets()
        
        for inlet in self.inlets:
            inlet.on_draw()
        for outlet in self.outlets:
            outlet.on_draw()
        
#    ####### DEBUG ########
#    def on_touch_down(self, touches, touchID, x, y): 
#        MTSelectableObject.on_touch_down(self, touches, touchID, x, y)     
#        #for inlet in self.inlets:
#        print self.width
#    ####### DEBUG ########

#    def on_touch_move(self, touches, touchID, x, y): 
#        MTSelectableObject.on_touch_move(self, touches, touchID, x, y) 
#        
#        isDragging, fingerID = self.dragging 
#        if isDragging:
#            return
#            #self.update_inlets()
#            
#            #for inlet in self.inlets:
#            #    inlet.x+=(self.x-self.oldX)
#            #    inlet.y+=(self.y-self.oldY)
#            #for outlet in self.outlets:
#            #    outlet.x+=(self.x-self.oldX)
#            #    outlet.y+=(self.y-self.oldY)
    
